﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.UpdatePackageTool
{
    /// <summary>
    /// BasicInfoControl.xaml 的交互逻辑
    /// </summary>
    public partial class BasicInfoControl : System.Windows.Controls.UserControl
    {
        public static Dictionary<string, List<System.Windows.Controls.UserControl>> basicInfoDictionary;
        public static OtherInfoControl _otherInfoControl;
        public static IEnumerable<FileInfo> OriginalVersionFiles { get; set; }
        public static IEnumerable<FileInfo> CurrentVersionFiles { get; set; }
        public static IEnumerable<FileInfo> FullCurrentVersionFiles { get; set; }
        public static string _createFileName;
        public static string _RootFolder { get; set; }
        private static ContentControl _contentControl;
        public BasicInfoControl()
        {
            InitializeComponent();
            basicInfoDictionary = new Dictionary<string, List<System.Windows.Controls.UserControl>>();
   
         
           
        }

        public BasicInfoControl(ContentControl contentControl)
        {
            _contentControl = contentControl;
            InitializeComponent();
            basicInfoDictionary = new Dictionary<string, List<System.Windows.Controls.UserControl>>();
     
          
           
        }

     

      

      
      

       
        private void _ctlBtnUpload_Click(object sender, RoutedEventArgs e)
        {

            _RootFolder = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = _ctlFileName.Text;
            fbd.Description = "请选择新版本文件";
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                _ctlFileName.Text = fbd.SelectedPath;
                OriginalVersionFiles = FileCompare.GetFiles(fbd.SelectedPath);
                int lastIndex = fbd.SelectedPath.LastIndexOf("\\");
                _RootFolder = fbd.SelectedPath.Substring(lastIndex + 1, fbd.SelectedPath.Length - lastIndex - 1);

            }

        }


        private void _ctlBtnCurrentUpload_Click(object sender, RoutedEventArgs e)
        {

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            Environment.SpecialFolder specialFolder = new Environment.SpecialFolder();

            fbd.RootFolder = specialFolder;
            fbd.Description = "请选原版本文件";
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                _ctlCurrentFileName.Text = fbd.SelectedPath;
                CurrentVersionFiles = FileCompare.GetFiles(fbd.SelectedPath);
            }


        }

        private void _ctlBtnCreateUpload_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            fbd.Description = "请选择生成更新包的路径";
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                _ctlCreateFileName.Text = fbd.SelectedPath;
                _createFileName = fbd.SelectedPath;
            }

        }

        private void _ctlBtnNext_Click(object sender, RoutedEventArgs e)
        {
            _ctlLblMessage.Content = "";

            if (!string.IsNullOrEmpty(_ctlFileName.Text.Trim()))
            {
                if (Directory.Exists(_ctlFileName.Text.Trim()))
                {

                    OriginalVersionFiles = FileCompare.GetFiles(_ctlFileName.Text.Trim());
                    int lastIndex = _ctlFileName.Text.Trim().LastIndexOf("\\");
                    _RootFolder = _ctlFileName.Text.Trim().Substring(lastIndex + 1, _ctlFileName.Text.Trim().Length - lastIndex - 1);
                }
                else
                {
                    _ctlLblMessage.Content = "* 新版本文件路径不存在！";
                    return;
                }
            }
            else
            {
                _ctlLblMessage.Content = "* 新版本文件路径不能为空！";
            }

            if (!string.IsNullOrEmpty(_ctlCurrentFileName.Text.Trim()))
            {
                if (Directory.Exists(_ctlCurrentFileName.Text.Trim()))
                {
                    CurrentVersionFiles = FileCompare.GetFiles(_ctlCurrentFileName.Text.Trim());
                }
                else
                {
                    _ctlLblMessage.Content = "* 原版本文件路径不存在！";
                    return;
                }
            }
            else
            {
                _ctlLblMessage.Content = "* 原版本文件路径不能为空！";
                return;
            }

            if (!string.IsNullOrEmpty(_ctlCreateFileName.Text.Trim()))
            {
                if (Directory.Exists(_ctlCreateFileName.Text.Trim()))
                {
                    _createFileName = _ctlCreateFileName.Text;
                }
                else
                {
                    _ctlLblMessage.Content = "* 更新包的路径不存在！";
                    return;
                }
            }
            else
            {
                _ctlLblMessage.Content = "* 更新包的路径不能为空！";
                return;
            }

            if (string.IsNullOrEmpty(_ctlNewVerNUM.Text.Trim()))
            {
                _ctlLblMessage.Content = "* 新版版本号不能为空！";
                return;
            }
            if (string.IsNullOrEmpty(_ctlOldVerNUM.Text.Trim()))
            {
                _ctlLblMessage.Content = "* 原版版本号不能为空！";
                return;
            }

            Product product = this.DataContext as Product;
            List<System.Windows.Controls.UserControl> controls;
            MainWindow.MainDictionary.TryGetValue(product.AppType, out controls);
            _contentControl.Content = controls[1];
        }
    }
}
